import { Module } from '@nestjs/common';
import { MicroEmailController } from './micro-email.controller';
import { MicroEmailService } from './micro-email.service';
import {join} from "path";
import {HandlebarsAdapter} from "@nestjs-modules/mailer/dist/adapters/handlebars.adapter";
import {BullModule} from "@nestjs/bull";
import {MailerModule} from "@nestjs-modules/mailer";
import {MicroEmailProcessor} from "./micro-email.processor";
import { ConfigModule, ConfigService } from '@nestjs/config';
import * as process from 'process';

@Module({
  imports: [
    ConfigModule.forRoot({isGlobal:true}),
    // 配置 Bull 模块
    BullModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        redis: {
          host: configService.get('REDIS_HOST'),
          port: configService.get('REDIS_PORT'),
          password: configService.get('REDIS_PASSWORD'),
        },
      }),
      inject: [ConfigService],
    }),
    // 注册队列
    BullModule.registerQueue({
      name: 'smtp',
    }),
    //导入MailerModule
    MailerModule.forRootAsync({
      imports:[ConfigModule],
      useFactory:async (configService:ConfigService)=>({
        transport:{
          host: configService.get('MAIL_HOST'),
          port: 465,
          secure: true, // use SSL if required
          auth: {
            user: configService.get('MAIL_USER'),
            pass: configService.get('MAIL_PASSWORD'),
          },

        },
        defaults: {
          from: configService.get('MAIL_FROM'),
          subject:'Yuzu-art',
        },
        template:{
          dir:join(process.cwd(),'templates'),
          adapter:new HandlebarsAdapter(),
        }
      }),
      inject:[ConfigService],



    })
  ],
  controllers: [MicroEmailController],
  providers: [MicroEmailProcessor,MicroEmailService],
})

export class MicroEmailModule {}
